package com.sbr.seb.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sbr.seb.entity.Log;
import com.sbr.seb.entity.server.Tomcat;
import com.sbr.seb.reader.LogReader;
import com.sbr.seb.util.UrlConnector;



public class LogServlet extends HttpServlet {

	private static final Logger logger = LoggerFactory.getLogger(LogServlet.class);
	


	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		Tomcat tomcat = new Tomcat();
		tomcat.setHost(request.getParameter("host"));
		tomcat.setPort(request.getParameter("port"));
		UrlConnector connector = new UrlConnector();

		BufferedReader in = connector.getReader(tomcat.getLogUrl());

		LogReader reader = new LogReader();

		List<Log> logList = reader.read(in);

		logger.debug("logList = "+logList.size());
		
		in.close();

		request.setAttribute("logList", logList);
		request.getRequestDispatcher("logList.jsp").forward(request,response);

	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}


}
